package com.ruoyi.gas.service.impl;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ruoyi.base.domain.Customer;
import com.ruoyi.base.domain.GasBalance;
import com.ruoyi.base.mapper.CustomerMapper;
import com.ruoyi.base.mapper.GasBalanceMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.gas.domain.GetInRecord;
import com.ruoyi.gas.domain.ScheduleRegister;
import com.ruoyi.gas.mapper.GetInRecordMapper;
import com.ruoyi.sale.domain.SaleRecord;
import com.ruoyi.sale.mapper.SaleRecordMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gas.mapper.FullfillRecordMapper;
import com.ruoyi.gas.domain.FullfillRecord;
import com.ruoyi.gas.service.IFullfillRecordService;

/**
 * 充装记录
Service业务层处理
 *
 * @author h w
 * @date 2022-12-09
 */
@Service
public class FullfillRecordServiceImpl implements IFullfillRecordService
{

    private static final String CHECK_STATUS_PASS = "1";
    private static final String CHECK_STATUS_FAIL = "2";

    @Autowired
    private FullfillRecordMapper fullfillRecordMapper;
    @Autowired
    private GetInRecordMapper getInRecordMapper;
    @Autowired
    private GasBalanceMapper gasBalanceMapper;
    @Autowired
    private SaleRecordMapper saleRecordMapper;
    @Autowired
    private CustomerMapper customerMapper;

    /**
     * 查询充装记录

     *
     * @param fullfillId 充装记录
主键
     * @return 充装记录

     */
    @Override
    public FullfillRecord selectFullfillRecordByFullfillId(Long fullfillId)
    {
        return fullfillRecordMapper.selectFullfillRecordByFullfillId(fullfillId);
    }

    /**
     * 查询充装记录
列表
     *
     * @param fullfillRecord 充装记录

     * @return 充装记录

     */
    @Override
    public List<FullfillRecord> selectFullfillRecordList(FullfillRecord fullfillRecord) {
        List<FullfillRecord> fullfillRecords = fullfillRecordMapper.selectFullfillRecordList(fullfillRecord);
        for (FullfillRecord record : fullfillRecords) {
            record.setTotalAmount(record.getSalePrice().multiply(record.getFullfillAmount()).setScale(2, BigDecimal.ROUND_HALF_UP));
        }
        return fullfillRecords;
    }

    @Override
    public FullfillRecord todayTotal() {
        FullfillRecord param = new FullfillRecord();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        param.setRecordTime(calendar.getTime());
        calendar.add(Calendar.DATE, 1);
        param.setCreateTime(calendar.getTime());
        List<FullfillRecord> fullfillRecords = selectFullfillRecordList(param);
        FullfillRecord res = new FullfillRecord();
        res.setFullfillNum("今日合计");
        res.setFullfillAmount(BigDecimal.ZERO);
        res.setTotalAmount(BigDecimal.ZERO);
        for (FullfillRecord fullfillRecord : fullfillRecords) {
            res.setFullfillAmount(res.getFullfillAmount().add(fullfillRecord.getFullfillAmount()));
            res.setTotalAmount(res.getTotalAmount().add(fullfillRecord.getTotalAmount()));
        }
        return res;
    }

    @Override
    public List<FullfillRecord> selectFullfillRecordRecentList(FullfillRecord fullfillRecord)
    {
        return fullfillRecordMapper.selectFullfillRecordRecentList(fullfillRecord);
    }

    @Override
    public String selectMaxFullfillNum() {
        return fullfillRecordMapper.selectMaxFullfillNum();
    }

    /**
     * 新增充装记录

     *
     * @param fullfillRecord 充装记录

     * @return 结果
     */
    @Override
    public int insertFullfillRecord(FullfillRecord fullfillRecord)
    {
        if(fullfillRecord.getGetInId() != null) {
            GetInRecord getInRecord = new GetInRecord();
            getInRecord.setGetInId(fullfillRecord.getGetInId());
            getInRecord.setStatus("1");
            getInRecordMapper.updateGetInRecord(getInRecord);
        }
        fullfillRecord.setCreateTime(DateUtils.getNowDate());
        return fullfillRecordMapper.insertFullfillRecord(fullfillRecord);
    }

    /**
     * 修改充装记录（核销）

     *
     * @param fullfillRecord 充装记录

     * @return 结果
     */
    @Override
    public int updateFullfillRecord(FullfillRecord fullfillRecord) throws Exception {
        int res = 0;
        if(StringUtils.isNotEmpty(fullfillRecord.getCheckStatus())) {
            FullfillRecord record = fullfillRecordMapper.selectFullfillRecordByFullfillId(fullfillRecord.getFullfillId());
            if(CHECK_STATUS_PASS.equals(fullfillRecord.getCheckStatus())) {
                GasBalance balance = new GasBalance();
                balance.setCustomerId(record.getCustomerId());
                List<GasBalance> balanceList = gasBalanceMapper.selectGasBalanceList(balance);
                if(balanceList == null || balanceList.size() <= 0) {
                    return res;
                } else {
                    GasBalance balanceModify = balanceList.get(0);

                    Customer customer = customerMapper.selectCustomerByCustomerId(record.getCustomerId());
                    Date fullfillDate = record.getRecordTime();
                    BigDecimal salePrice = customer.getSalePrice();
                    if(fullfillDate.before(customer.getUpdateTime())) {
                        salePrice = customer.getLastSalePrice();
                    }
                    if(salePrice == null) {
                        throw new Exception("该客户没有维护销售单价，请检查！");
                    }
                    BigDecimal subtract = record.getFullfillAmount().multiply(salePrice);
                    BigDecimal mins = balanceModify.getCustomerBalance().subtract(subtract);
                    if(balanceModify.getCustomerId() == 9 && mins.compareTo(BigDecimal.ZERO) > 0) {
                        return res;
                    } else {
                        balanceModify.setCustomerBalance(mins);
                        balanceModify.setUpdateTime(DateUtils.getNowDate());
                        balanceModify.setUpdateBy(fullfillRecord.getUpdateBy());
                        gasBalanceMapper.updateGasBalance(balanceModify);
                        SaleRecord saleRecord = new SaleRecord();
                        saleRecord.setCustomerId(record.getCustomerId());
                        saleRecord.setCustomerName(record.getCustomerName());
                        saleRecord.setSalePrice(record.getSalePrice());
                        saleRecord.setFactAmount(record.getFullfillAmount());
                        saleRecord.setSaleAmount(subtract);
                        saleRecord.setSettleStatus("1");
                        saleRecord.setSettleTime(DateUtils.getNowDate());
                        saleRecord.setCarInfo(record.getCarHeadNum());
                        saleRecord.setTotalAmount(subtract);
                        saleRecord.setLossIncrease("0");
                        saleRecord.setFullfillId(record.getFullfillId());
                        saleRecord.setCreateBy(fullfillRecord.getUpdateBy());
                        saleRecord.setCreateTime(DateUtils.getNowDate());
                        saleRecordMapper.insertSaleRecord(saleRecord);
                        fullfillRecord.setUpdateTime(DateUtils.getNowDate());
                        return fullfillRecordMapper.updateFullfillRecord(fullfillRecord);
                    }
                }
            } else if(CHECK_STATUS_FAIL.equals(fullfillRecord.getCheckStatus())) {
                fullfillRecord.setUpdateTime(DateUtils.getNowDate());
                return fullfillRecordMapper.updateFullfillRecord(fullfillRecord);
            }
        }
        return res;
    }

    /**
     * 批量删除充装记录

     *
     * @param fullfillIds 需要删除的充装记录
主键
     * @return 结果
     */
    @Override
    public int deleteFullfillRecordByFullfillIds(Long[] fullfillIds)
    {
        return fullfillRecordMapper.deleteFullfillRecordByFullfillIds(fullfillIds);
    }

    /**
     * 删除充装记录
信息
     *
     * @param fullfillId 充装记录
主键
     * @return 结果
     */
    @Override
    public int deleteFullfillRecordByFullfillId(Long fullfillId)
    {
        return fullfillRecordMapper.deleteFullfillRecordByFullfillId(fullfillId);
    }
}
